package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.view.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.ui.PageScroller;
import zuo.biao.library.util.StringUtil;

/**
 * 使用方法：复制>粘贴>改名>改代码
 */

/**
 * activity示例
 *
 * @warn 复制到其它工程内使用时务必修改import R文件路径为所在应用包名
 * @use toActivity(DemoActivity.createIntent(...));
 */
public class Demo2Activity extends BaseActivity implements View.OnClickListener, OnBottomDragListener {
    private final String TAG = "DemoActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    public static final String INTENT_USER_ID = "INTENT_USER_ID";


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @param userId
     * @return
     */
    public static Intent createIntent(Context context, long userId) {
        return new Intent(context, Demo2Activity.class).putExtra(INTENT_USER_ID, userId);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    private long userId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO demo_activity改为你所需要的layout文件；传this是为了底部左右滑动手势
        setContentView(R.layout.demo_activity, this);

        intent = getIntent();
        userId = intent.getLongExtra(INTENT_USER_ID, userId);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

//        Toast.makeText(context, "这是一个分页列表，中速滑动直接滚动一页。\n如果不需要则把PageScroller相关代码去掉"
//                , Toast.LENGTH_LONG).show();
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lvDemo;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE

        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        lvDemo = (ListView) findViewById(R.id.lvDemo);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Map<String, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lvDemo.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Map<String, String>> list;

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                list = getList(userId);
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(list);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    /**
     * 示例方法：获取列表
     *
     * @param userId
     * @return
     */
    protected List<Map<String, String>> getList(long userId) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 64; i++) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", "联系人" + i + userId);
            map.put("number", String.valueOf(1311736568 + i * i + userId));
            map.put("age", (i * i + userId) + "");
            list.add(map);
        }
        return list;
    }

    /**
     * 示例方法：添加列表
     */
    private void addList() {
        int formerCout = adapter == null ? 0 : adapter.getCount() - 1;

        if (list == null) {
            list = new ArrayList<Map<String, String>>();
        }
        userId += list.size();
        list.addAll(getList(userId));

        if (adapter != null) {
            adapter.refresh(list);
        }
        lvDemo.smoothScrollToPosition(formerCout);
    }
    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lvDemo.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                setResult(RESULT_OK, new Intent().putExtra(RESULT_CLICKED_ITEM, position));
//                finish();

            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lvDemo).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }


    @Override
    public void onDragBottom(boolean rightToLeft) {
        if (rightToLeft) {
//            addList();
            return;
        }

        finish();
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法
    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.llUserTag:
//                toActivity(EditTextInfoActivity.createIntent(context, "标签"
//                        , StringUtil.getTrimedString(tvUserTag)), REQUEST_TO_EDIT_TEXT_INFO);
//                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

//    private final int REQUEST_TO_BOTTOM_MENU = 1;
//    private final int REQUEST_TO_EDIT_TEXT_INFO = 2;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
//        switch (requestCode) {
//            case REQUEST_TO_BOTTOM_MENU:
//                if (data != null) {
//                    onBottomMenuItemClick(data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1));
//                }
//                break;
//            case REQUEST_TO_EDIT_TEXT_INFO:
//                if (user == null) {
//                    user = new User(userId);
//                }
//                user.setTag(data == null ? null : data.getStringExtra(EditTextInfoActivity.RESULT_VALUE));
//                setUser(user);
//                break;
//        }
    }


    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Adapter
/** 使用方法：复制>粘贴>改名>改代码  */
    /**
     * adapter模板，最灵活且性能最好，但如果有setOnClickListener等事件就不方便了
     * <br> 适用于ListView,GridView等AbsListView的子类
     *
     * @use new DemoAdapter(...); 具体参考.DemoActivity(setList方法内)
     */
    public class DemoAdapter extends BaseAdapter<Map<String, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.demo_item, parent, false);
            }

            TextView tvDemoViewName = BaseViewHolder.get(convertView, R.id.tvDemoItemName);
            TextView tvDemoViewNumber = BaseViewHolder.get(convertView, R.id.tvDemoItemNumber);

            final Map<String, String> data = getItem(position);
            tvDemoViewName.setText(StringUtil.getTrimedString(data.get("name")));
            tvDemoViewNumber.setText(StringUtil.getNoBlankString(data.get("number")));

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }

}